
var Packet = function(x, y, health, speed, emitter, stage){
    var _textOffsetX = 1;
    var _textOffsetY = -8;
    
    var _x = x;
    var _y = y;
    var _health = health;
    var _maxHealth = health;
    var _shape;
    var _speed = speed;
    var _emitter = emitter;
    var _type = emitter.getType();
    var _destroyed = false;
    var _txt;
    
    var _callbackOnDestroy;
        
    //Creating the shape
    _shape = new createjs.Shape();
    
    _shape.x = x;
    _shape.y = y;
    
    // Concat the type of the emitter for automaticcaly get the image to display.
    var functionToCall = 'getPacket'+_type;
    
    // Generate image. (Ambroise)
    var _image = ImageEngine.getInstance()[functionToCall]();

    // Center the element.
    _shape.x = _x //- _image.width/2;
    _shape.y = _y //- _image.height/2;
    _shape.regX = _image.width/2;
    _shape.regY = _image.height/2;
    
    // Generate the graphic element.
    _shape.graphics.beginBitmapFill(_image).drawRect(0, 0, _image.width, _image.height);
    
    stage.addChild(_shape);
    
    this.getX = function(){
        return _x;
    };
    this.setX = function(x){
        _x = x;
        _shape.x = x;
        _txt.x = x+_textOffsetX;
 
    };
    this.getY = function(){
        return _y;
    };
    this.setY = function(y){
        _y = y;
        _shape.y = y;
        _txt.y = y+_textOffsetY;
    };
    this.getType = function() {
        return _type;
    }
    this.setType = function(t){
        _type = t;
        functionToCall = 'getPacket'+_type;
        _image = ImageEngine.getInstance()[functionToCall]();
        _shape.graphics.beginBitmapFill(_image).drawRect(0, 0, _image.width, _image.height);
    }
    this.getHealth = function() {
        return _health;
    }
    this.setHealth = function(h){
        _health = h;
        _txt.text = _health;
        
        if(_health > 66/100*_maxHealth){
            _txt.color = "#00CC22";
        } else if (_health > 33/100*_maxHealth){
            _txt.color = "orange";
        } else {
            _txt.color = "red";
        }
    };
    this.getMaxHealth = function(){
        return _maxHealth;
    };
    this.getShape = function(){
        return _shape;
    };
    this.decrementHealth = function(){
        if(_health>0){
            _health--;
            _txt.text = _health;
        }
    };
    this.getSpeed = function(){
        return _speed;
    }
    this.setSpeed = function(speed){
        _speed = speed;
    };
    this.getEmitter = function(){
        return _emitter;
    };
    this.getType = function(){
        return _type;
    };
    this.setCallbackOnDestroy = function(callback){
        _callbackOnDestroy = callback;
    };
    this.destroy = function(){
        SoundEngine.getInstance().ballDesintegration();
        
        _destroyed = true;
        _shape.visible = false;
        _txt.visible = false;
        
        if(_callbackOnDestroy != null){
            _callbackOnDestroy(this);
        }
        
    };
    
    this._reinit = function(x, y, health, speed, emitter){
        this.setX(x);
        this.setY(y);
        _maxHealth = health;
        this.setHealth(health);
        _speed = speed;
        _emitter = emitter;
        _shape.visible = true;
        _txt.visible = true;
        
        return this;
    }
    
    this.hasBeenDestroyed = function(){
        return _destroyed;
    }
    
    _txt = new createjs.Text(_health, "13px Verdana");
    _txt.textAlign = "center";
    _txt.x = _x+_textOffsetX;
    _txt.y = _y+_textOffsetY;//hauteur de l'image à la louche
    //pour mettre la bonne couleur
    this.setHealth(_health);
    
    stage.addChild(_txt);
    
};